package com.dhx.controller;

import com.dhx.pojo.Student;
import com.dhx.pojo.Teacher;
import com.dhx.service.StudentService;
import com.dhx.service.impl.StudentServiceimpl;
import com.dhx.service.impl.TeacherServiceimpl;
import com.utils.DistanceUtil;
import com.utils.request;
import com.utils.timeutil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.jws.WebParam;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private TeacherServiceimpl teacherServiceimpl;
    @Autowired
    private StudentServiceimpl studentServiceimpl;

    @RequestMapping("/saveyy")
    public ModelAndView save(Student student) {
        student.setTime(new Date());
        SimpleDateFormat df = new SimpleDateFormat("HH:mm");// 设置日期格式
        Date now = null;
        Date beginTime = null;
        Date endTime = null;
        try {
            now = df.parse(df.format(new Date()));
            beginTime = df.parse("00:01");
            endTime = df.parse("12:00");
        } catch (Exception e) {
            e.printStackTrace();
        }
        Boolean flag = timeutil.belongCalendar(now, beginTime, endTime);
        if (flag == true) {
            student.setFlag(1);
        } else {
            student.setFlag(0);
        }
        request request = new request();
        String xian = request.getRequest().getParameter("xian");
        String jd = request.getRequest().getParameter("jd");
        String wd = request.getRequest().getParameter("wd");
        System.out.println(student);
        if (xian.equals("1")) {
            ModelAndView modelAndView = new ModelAndView();
            System.out.println("111");
            //科三 c1 1号线  待考区附近
            //test home 113.497941,38.040595
            double distance = DistanceUtil.getDistance(Double.parseDouble(jd), Double.parseDouble(wd), 113.497941, 38.040595);
            System.out.println(distance);
            if ((distance <= 1000) && (!(student.getName().equals("")))) {
                studentServiceimpl.saveyy(student);
                modelAndView.addObject("msg", "签到成功！");
            } else {
                modelAndView.addObject("errormsg", "签到失败！");
            }
            modelAndView.addObject("lon", 113.497941);
            modelAndView.addObject("lat", 38.040595);
            modelAndView.setViewName("1qiandao");
            return modelAndView;
        } else if (xian.equals("2")) {
            System.out.println("222");
            //科三 c1 2号线  武装部
            double distance = DistanceUtil.getDistance(Double.parseDouble(jd), Double.parseDouble(wd), 113.444360, 38.082973);
            if (distance <= 200) {
                studentServiceimpl.saveyy(student);
            }
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.addObject("lon", 113.444360);
            modelAndView.addObject("lat", 38.082973);
            modelAndView.setViewName("1qiandao");
            return modelAndView;
        } else if (xian.equals("3")) {
            System.out.println("333");
            //科三 c1 3号线  实际定位
            double distance = DistanceUtil.getDistance(Double.parseDouble(jd), Double.parseDouble(wd), 113.447768, 38.094727);
            if (distance <= 200) {
                studentServiceimpl.saveyy(student);
            }
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.addObject("lon", 113.447768);
            modelAndView.addObject("lat", 38.094727);
            modelAndView.setViewName("1qiandao");
            return modelAndView;
        } else if (xian.equals("4")) {
            System.out.println("444");
            ModelAndView modelAndView = new ModelAndView();
            //科二 c1 c2  4号线  东坪驾校
            double distance = DistanceUtil.getDistance(Double.parseDouble(jd), Double.parseDouble(wd), 113.4509199, 38.077048);
            double distance2 = DistanceUtil.getDistance(Double.parseDouble(jd), Double.parseDouble(wd), 113.451043, 38.077091);
            double distance3 = DistanceUtil.getDistance(Double.parseDouble(jd), Double.parseDouble(wd), 113.450241, 38.077752);
            double distance4 = DistanceUtil.getDistance(Double.parseDouble(jd), Double.parseDouble(wd), 113.451401, 38.077231);
            double distance5 = DistanceUtil.getDistance(Double.parseDouble(jd), Double.parseDouble(wd), 113.450718, 38.076874);
            if ((distance <= 500 || distance2 <= 500 || distance3 <= 500 || distance4 <= 500 || distance5 <= 500) && (!(student.getName().equals("")))) {
                studentServiceimpl.saveyy(student);
                modelAndView.addObject("msg", "签到成功！");
            } else {
                modelAndView.addObject("errormsg", "签到失败！请检查框中是否已输入姓名或者定位离驾校太远！");
            }
            modelAndView.addObject("lon", 113.4509199);
            modelAndView.addObject("lat", 38.077048);
            modelAndView.setViewName("1qiandao");
            return modelAndView;
        }
        return null;
    }

    @RequestMapping("/findyy")
    public ModelAndView finyy() {
        request request = new request();
        String id = request.getRequest().getParameter("id");
        String flag = request.getRequest().getParameter("flag");
        Date date = new Date();  // 获取当前系统时间
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  // 设置日期格式
        String strTime = simpleDateFormat.format(date);  // 格式转换
        List<Student> students = null;
        if (flag.equals("all")) {
            students = studentServiceimpl.findyy(id, strTime);
        } else if (flag.equals("1")) {
            students = studentServiceimpl.findyyam(id, strTime);
        } else if (flag.equals("0")) {
            students = studentServiceimpl.findyypm(id, strTime);
        }
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("cp", id);
        modelAndView.addObject("stulist", students);
        modelAndView.setViewName("list");
        return modelAndView;
    }


    @RequestMapping("/findjl")
    public ModelAndView finjl() {
        request request = new request();
        String id = request.getRequest().getParameter("id");
        String model = request.getRequest().getParameter("model");
        List<Teacher> teachers = teacherServiceimpl.findjl(id, model);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("tealist", teachers);
        modelAndView.setViewName("jllist");
        return modelAndView;
    }

}
